//tab切换
layui.use('element', function () {
    let element = layui.element;
});

//加载时间控件
layui.use('laydate', function () {
    let date = layui.laydate;
    //执行一个date实例
    date.render({
        elem: '#date',  //指定元素
        type: "datetime",
        format: "yyyy年MM月dd日 HH时mm分ss秒",
        theme: "blue"
    });
});

//标记
let flag = false;
//加载表格
layui.use('table', function () {
    let table = layui.table;
    table.render({
        elem: '#drug',
        id: "drug",
        height: 520,
        url: "/drug/drugPage",
        method: 'post',
        page: true,
        limit: 10,
        toolbar: "#toolbarDemo2",  //表头工具栏
        cols: [[
            {field: 'id', title: '药品编号', width: 120, sort: true, fixed: 'left'},
            {field: 'drugName', title: '药品名称', width: 140},
            {
                field: 'type', title: '药品类型', width: 120, templet: function (d) {
                    return d.type === 1 ? '中药' : '西药';
                }
            },
            {field: 'utils', title: '药品单位', width: 120},
            {field: 'price', title: '药品价格', width: 120, sort: true, edit: 'text'},
            {
                field: 'count', title: '库存数量', width: 120, sort: true, templet: function (d) {
                    return d.count;
                }
            },
            {field: 'remark', title: '药品描述', width: 400, edit: 'text'},
            {
                field: 'show', title: '库存状态', width: 120, templet: function (d) {
                    return d.count < 100 ? "<span style='color: #e72727'>库存不足</span>" : "<span style='color:#35f535'>库存充足</span>"
                }
            },

            {field: 'tool', title: '操作', width: 140, toolbar: "#barDemo"}
        ]],
        text: {
            none: '暂无数据'
        },
        where: {
            drugName: "",    //药品名称
            drugType: 0          //药品类型
        },
        done: function (res) {
            if (flag) {
                return;
            }
            let data = res.data;
            if (data.length === 0) {
                return;
            }
            flag = true;
            //查询库存状态
            $.ajax({
                url: "/drug/drugList",
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data.length <= 0) {
                        return;
                    }
                    let lis = ``;     //数据集合

                    $.each(data, function (index, val) {
                        if (val.count < 100) {
                            lis += `<li class="liuis">
                                        <input type="checkbox" class="check">
                                        <span class="span">${val.id}</span>
                                        <span class="span">${val.drugName}</span>
                                        <span class="span">${val.count}</span>
                                    </li>`
                        }
                    });

                    let div = `<div class="kun">
                                    <h2 class="title">库存名单</h2>
                                    <div>
                                          <ul>
                                            <li class="liui">
                                                <input type="checkbox" class="check">
                                                <span class="span">药品编号</span>
                                                <span class="span">药品名称</span>
                                                <span class="span">剩余库存</span>
                                            </li>
                                            ${lis}
                                          </ul>
                                    </div>
                               </div>`;
                    //弹出层
                    layer.open({
                        id: 'hint',                //设定一个id，防止重复弹出
                        type: 1,                   //弹出层类型
                        title: false,              //不显示标题栏
                        content: div,              //弹出层内容
                        closeBtn: false,           //关闭按钮
                        resize: false,             //是否允许窗口拉伸
                        btn: ['申请入库', '全部关闭'],      //按钮
                        btnAlign: 'c',             //按钮居中
                        moveType: 1,               //拖拽模式，0或者1
                        yes: function (index, layero) {
                            //创建药单
                            let len = $("body .kun .liuis").find(".check:checked").length;
                            //判断复选框选择的数量如果为0就跳过
                            if (len === 0) {
                                layer.close(index);
                                return;
                            }
                            // 重新加载模块
                            layui.use('element', function () {
                                let element = layui.element;
                                element.tabChange('synthesis', 'stork');
                            });
                            let drugNames = "";
                            $("body .kun .liuis").find(".check:checked").each(function () {
                                let drugID = $(this).next().text();
                                let drugName = $(this).next().next().text();
                                drugNames += " " + drugName + " ";
                                let li = `<li class="liui2">
                                          <div class="div2">
                                            <span class="span2">${drugID}</span>
                                            <span class="span2">${drugName}</span>
                                            <span class="span2">
                                                <input type="text" class="input">
                                            </span>
                                            <span class="span2">
                                                <a href="javascript:void(0)">删除</a>
                                            </span>
                                          </div>
                                      </li>`;
                                $(".divui>ul").append(li);
                            })
                            //关闭弹出层
                            layer.close(index);
                            //发送短信
                            // $.ajax({
                            //     url: "http://localhost:8080/com/initServlet",
                            //     type: "post",
                            //     data: {
                            //         opr: "massage",
                            //         drugNames: drugNames
                            //     },
                            //     dataType: "json",
                            //     success: function (data) {
                            //
                            //     }
                            // })
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                        }
                    })
                }
            })
        }
    })

    //工具头  做了查询和刷新
    table.on('toolbar(drugs)', function (obj) {
        let drugName = $("#drugname").val();
        let drugType = $("#drugtype").val();
        switch (obj.event) {
            case 'sel':
                table.reload('drug', {
                    page: {
                        curr: 1
                    },
                    where: {
                        drugName: drugName,
                        drugType: drugType
                    }
                })
                break;
            case 'upd':
                table.reload('drug', {
                    where: {
                        drugName: drugName,
                        drugType: drugType
                    }
                })
                break;
            case 'check':
                //查询库存状态
                $.ajax({
                    url: "/drug/drugList",
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.length === 0) {
                            layer.msg("当前库存充足")
                            return;
                        }
                        let lis = ``;     //数据集合

                        $.each(data, function (index, val) {
                            if (val.count < 100) {
                                lis += `<li class="liuis">
                                        <input type="checkbox" class="check">
                                        <span class="span">${val.id}</span>
                                        <span class="span">${val.drugName}</span>
                                        <span class="span">${val.count}</span>
                                    </li>`
                            }
                        });

                        let div = `<div class="kun">
                                      <h2 class="title">库存名单</h2>
                                      <div>
                                          <ul>
                                            <li class="liui">
                                                <input type="checkbox" class="check">
                                                <span class="span">药品编号</span>
                                                <span class="span">药品名称</span>
                                                <span class="span">剩余库存</span>
                                            </li>
                                            ${lis}
                                          </ul>
                                      </div>
                                   </div>`;
                        //弹出层
                        layer.open({
                            id: 'hint2',                //设定一个id，防止重复弹出
                            type: 1,                   //弹出层类型
                            title: false,              //不显示标题栏
                            content: div,              //弹出层内容
                            closeBtn: false,           //关闭按钮
                            resize: false,             //是否允许窗口拉伸
                            btn: ['申请入库', '全部关闭'],      //按钮
                            btnAlign: 'c',             //按钮居中
                            moveType: 1,               //拖拽模式，0或者1
                            yes: function (index, layero) {
                                let len = $("body .kun .liuis").find(".check:checked").length;
                                //判断复选框选择的数量如果为0就跳过
                                if (len === 0) {
                                    //关闭弹出层
                                    layer.close(index);
                                    return;
                                }
                                // 重新加载模块
                                layui.use('element', function () {
                                    let element = layui.element;
                                    element.tabChange('synthesis', 'stork');
                                });
                                let drugNames = "";
                                //创建药单
                                $("body .kun .liuis").find(".check:checked").each(function () {
                                    let drugID = $(this).next().text();
                                    let drugName = $(this).next().next().text();
                                    drugNames += " " + drugName + " ";
                                    let li = `<li class="liui2">
                                          <div class="div2">
                                            <span class="span2">${drugID}</span>
                                            <span class="span2">${drugName}</span>
                                            <span class="span2">
                                                <input type="text" class="input">
                                            </span>
                                            <span class="span2">
                                                <a href="javascript:void(0)">删除</a>
                                            </span>
                                          </div>
                                      </li>`;
                                    $(".divui>ul").append(li);
                                })
                                //关闭弹出层
                                layer.close(index);
                                //短信通知
                                // $.ajax({
                                //     url: "http://localhost:8080/com/initServlet",
                                //     type: "post",
                                //     data: {
                                //         opr: "massage",
                                //         drugNames: drugNames
                                //     },
                                //     dataType: "json",
                                //     success: function (data) {
                                //
                                //     }
                                // })
                            },
                            btn2: function (index, layero) {
                                layer.close(index);
                            }
                        })
                    }
                })
                break;
        }
    })

    //单元格编辑  单独更改字段
    table.on('edit(drugs)', function (obj) {
        $.ajax({
            url: "/drug/updDrug",
            type: "post",
            data: {
                id: obj.data.id,    //药品编号
                field: obj.field,   //字段名
                value: obj.value    //属性值
            },
            dataType: 'json',
            success: function (data) {

            }
        })
    })

    //行工具条
    table.on('tool(drugs)', function (obj) {
        let choo = obj.event;    //拿到事件名
        let date = obj.data;
        switch (choo) {
            case 'decl':
                layer.prompt({
                    formType: 2,
                    title: '药品描述',
                    value: date.remark
                }, function (value, index) {
                    $.ajax({
                        url: "/drug/updDrug",
                        type: "post",
                        data: {
                            id: date.id,      //药品编号
                            field: "remark",  //字段 [备注]
                            value: value      //值
                        },
                        dataType: 'json',
                        success: function (data) {

                        }
                    })
                    obj.update({
                        remark: value
                    })
                    layer.close(index);
                })
                break;
            case 'del':
                layer.msg('删除药品请与管理人员联系')
                break;
        }
    })
})
//加载表单
layui.use('form', function () {
    let form = layui.form;
    form.render('select', 'test1');
});